package cn.cmcc.online.smsapi.ui.web.help;

import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import cn.cmcc.online.smsapi.ui.web.widget.SdkWebView;

/**
 * 帮助WebView处理各种通知、请求事件的
 */
public class WebViewClientHelper extends WebViewClient {
    private static final String TAG = WebViewClientHelper.class.getSimpleName();

    @Override
    public void onLoadResource(WebView view, String url) {
        try {
            if (view instanceof SdkWebView) {
                SdkWebView webView = (SdkWebView) view;
                webView.injectJavascriptInterfaces();
            }
        } catch (Throwable e) {
            Log.e(TAG, "onLoadResource e =" + e);
        }
        super.onLoadResource(view, url);
    }

    @Override
    public void doUpdateVisitedHistory(WebView view, String url,
                                       boolean isReload) {
        try {
            if (view instanceof SdkWebView) {
                SdkWebView webView = (SdkWebView) view;
                webView.injectJavascriptInterfaces();
            }
        } catch (Throwable e) {
            Log.e(TAG, "doUpdateVisitedHistory, e =" + e);
        }
        super.doUpdateVisitedHistory(view, url, isReload);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        try {
            if (view instanceof SdkWebView) {
                SdkWebView webView = (SdkWebView) view;
                webView.injectJavascriptInterfaces();
            }
        } catch (Throwable e) {
            Log.e(TAG, "onPageStarted, e =" + e);
        }
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        try {
            if (view instanceof SdkWebView) {
                SdkWebView webView = (SdkWebView) view;
                webView.injectJavascriptInterfaces();
            }
        } catch (Throwable e) {
            Log.e(TAG, "onPageFinished, e =" + e);
        }
        super.onPageFinished(view, url);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
                                String description, String failingUrl) {
        try {
            if (view instanceof SdkWebView) {
                SdkWebView webView = (SdkWebView) view;
                webView.injectJavascriptInterfaces();
            }
        } catch (Throwable e) {
            Log.e(TAG, "onReceivedError, e =" + e);
        }
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        try {
            if (view instanceof SdkWebView) {
                SdkWebView webView = (SdkWebView) view;
                webView.injectJavascriptInterfaces();
            }
        } catch (Throwable e) {
            Log.e(TAG, "shouldOverrideUrlLoading, e =" + e);
        }
        return super.shouldOverrideUrlLoading(view, url);
    }

}
